package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.project.dao.SoilTemperatureDataDAO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.entity.SoilTemperatureData;
import edu.ynu.se.xiecheng.achitectureclass.project.po.vo.EnvironmentVO;
import edu.ynu.se.xiecheng.achitectureclass.project.po.vo.SoilTemperatureVO;
import edu.ynu.se.xiecheng.achitectureclass.project.service.SoilTemperatureDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/st")
@Api(tags = "土壤温度数据管理")
public class SoilTemperatureDataController extends LogicController<SoilTemperatureDataService, SoilTemperatureDataDAO, SoilTemperatureData, Long> {

    public SoilTemperatureDataController(SoilTemperatureDataService ls) {
        super(ls);
    }


    @ApiOperation("获取最新的土壤温度数据")
    @GetMapping("/latest")
    public ResponseEntity<SoilTemperatureVO> getLatestSoilTemperatureData() {
        try {
            SoilTemperatureVO data = service.getLatestSoilTemperatureData();
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @PostMapping("/fetch")
    @ApiOperation("从传感器获取并存储土壤温度数据")
    public ResponseEntity<String> fetchAndStoreTemperatureData() {
        try {
            service.fetchAndSaveSoilTemperatureData(); // 调用 Service 层方法
            return ResponseEntity.ok("土壤温度数据成功获取并存储！");
        } catch (Exception e) {
            return ResponseEntity.internalServerError()
                    .body("获取并存土壤温度数据时发生错误：" + e.getMessage());
        }
    }

}
